package org.hot100_5.leetcode20;


import java.util.HashMap;
import java.util.Stack;

public class Solution {
    public boolean isValid(String s) {
        Stack<Character> stack = new Stack<>();
        int n = s.length();
        HashMap<Character, Character> hashMap = new HashMap<>();
        hashMap.put(')', '(');
        hashMap.put('}', '{');
        hashMap.put(']', '[');

        for (int i = 0; i < n; i++) {

            if (!hashMap.containsKey(s.charAt(i))) {
                stack.add(s.charAt(i));
            } else {
                if (stack.isEmpty() || stack.peek() != hashMap.get(s.charAt(i))) {
                    return false;
                }
                stack.pop();
            }
        }
        return stack.isEmpty();
    }
}
